﻿/*
 * Copyright 2020, Guan Xiaopeng
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
 
using System;
using System.Net;

namespace XPRPC
{
    /// <summary>
    /// Service descriptor to declare a service in service manager
    /// </summary>
    [Serializable]
    public sealed class ServiceDescriptor
    {
        /// <summary>
        /// The unique name to identify a service in service manager
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// The interface name of the service
        /// </summary>
        public string InterfaceName { get; set; }

        /// <summary>
        /// Description of the service.
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// The name of host where the service's running.
        /// This is ignored for local service.
        /// </summary>
        public string ServiceHost {get; set;}
        /// <summary>
        /// The TCP port at which the service listening.
        /// This is ignored for local service.
        /// </summary>
        public int ServicePort { get; set; }

        /// <summary>
        /// If the TCP connection shall use SSL.
        /// </summary>
        public bool UseSSL {get; set;} = false;

        /// <summary>
        /// The access token generated by service.
        /// Clients get this by call @IServiceManager.GetService to access the service.
        /// The service will reject clients without the token.
        /// </summary>
        public string AccessToken { get; set; }

        public override string ToString()
        {
            return $"{Name}<{InterfaceName}>@{ServiceHost}:{ServicePort}";
        }

        /// <summary>
        /// Check if a descriptor is same with another
        /// </summary>
        /// <param name="other">the another instance</param>
        /// <returns></returns>

        public bool IsIdentityWith(ServiceDescriptor other)
        {
            return this.ToString() == other.ToString();
        }

        public bool IsValid => !string.IsNullOrEmpty(AccessToken);
    }
}

